<script>
import { uuid, formatDate, getAddress } from '@/utils/utils.js';

export default {
	data() {
		return {
			netWorkAppVersion: null
		};
	},
	onLaunch: function () {
		uni.getPushClientId({
			success: (res) => {
				let push_clientid = res.cid;
			},
			fail(err) {
				console.log(err);
			}
		});
		uni.onPushMessage((res) => {
			console.log('收到推送消息：', res); //监听推送消息
			// #ifdef APP-PLUS
			uni.createPushMessage({
				title: res.data.title,
				content: res.data.content,
				payload: res.data.payload
			});
			// #endif
			// #ifdef H5
			this.handleTransYuYin(res.data.title);
			// #endif
		});
		let token = JSON.parse(JSON.stringify(uni.getStorageSync('token')));
		if (!token) {
			uni.reLaunch({
				url: '/pages/login/login'
			});
		}
	},
	onShow: function () {
		if (process.env.NODE_ENV === 'development') {
		} else {
			this.getVisionTitle();
		}
		// let websocketUrl = 'ws://172.16.90.202:8000/websocket/';
		// // #ifdef H5
		// websocketUrl = 'ws://172.16.90.202:8000/websocket/';
		// // #endif
		// // #ifdef APP-PLUS
		// websocketUrl = 'ws://wl.antaigroup.com:8795/websocket/';
		// // #endif
		// // 首先建立websocket连接
		// const socketTask = uni.connectSocket({
		// 	url: websocketUrl + '1179455164323598336',
		// 	complete: () => {}
		// });
		// uni.onSocketOpen(function (res) {
		// 	console.log('WebSocket连接已打开！');
		// 	uni.onSocketMessage(function (res) {
		// 		console.log('收到服务器内容：' + res.data);
		// 	});
		// });
	},
	onHide: function () {},
	// globalData: {
	// 	baseUrl: 'http://localhost:8080',
	// 	gaoDeKey: '7277a24b0f32e0dace6157d671d24c32',
	// 	baseSsoUrl: 'http://172.16.90.214:80',
	// 	h5WebsocketUrl: 'ws://172.16.90.214:8000/websocket/',
	// 	appWebsocketUrl: 'ws://172.16.90.214:8000/websocket/'
	// }

	methods: {
		handleTransYuYin(val) {
			let msg = new SpeechSynthesisUtterance(val);
			window.speechSynthesis.speak(msg); // msg.volume = 1000; // msg.rate = 1; // msg.pitch = 1;
		},
		async getVisionTitle() {
			//获取版本
			await this.getVersion();
			const res = uni.getAppBaseInfo();
			uni.setStorageSync('appVersion', res.appVersion);
			if (this.netWorkAppVersion && this.netWorkAppVersion != res.appVersion) {
				let that = this;
				let tipTile = `当前版本较低(${res.appVersion}),请立即更新版本(${this.netWorkAppVersion})`;
				uni.showModal({
					title: '版本更新提醒',
					content: tipTile,
					showCancel: false,
					confirmText: '立即更新',
					success: (res) => {
						if (res.confirm) {
							// #ifdef APP-PLUS
							that.downloadApp();
							// #endif
							// #ifdef H5
							window.location.reload();
							// #endif
						} else {
							uni.showToast({
								duration: 3000,
								title: '请尽快更新,后续低版本将无法使用!',
								icon: 'none'
							});
						}
					}
				});
			}
		},
		// 获取版本
		async getVersion() {
			const res = await this.$myRequest({
				url: '/api/v1/uc/getAppVersion',
				method: 'POST'
			}).catch((err) => {
				this.netWorkAppVersion = null;
			});
			if (res.statusCode == '200') {
				this.netWorkAppVersion = res.data.data;
			} else {
				this.netWorkAppVersion = null;
			}
		},
		downloadApp() {
			uni.showLoading({
				title: '更新中,请耐心等待',
				mask: true
			});
			//对比版本号
			uni.downloadFile({
				url: 'http://wl.antaigroup.com:8795/data/file/物流平台.apk',
				success: (downloadResult) => {
					uni.hideLoading();
					if (downloadResult.statusCode === 200) {
						plus.runtime.install(
							downloadResult.tempFilePath,
							{
								force: false
							},
							function () {
								// 记录热更新文件的版本号
								uni.setStorageSync('version2', data.hotupdate_version);
								uni.showModal({
									title: 'app更新已就绪是否立即更新',
									content: '',
									success: (showResult) => {
										if (showResult.confirm) {
											plus.runtime.restart();
										}
									}
								});
							},
							function (e) {}
						);
					}
				},
				fail: (downloadResult) => {}
			});
		}
	}
};
</script>
<style>
/* @import '/static/iconfont.css'; */
/* #ifndef APP-NVUE */
@import url('/static/iconfont.css');
/* #endif */
/* @import url('https://at.alicdn.com/t/c/font_4484473_vxoqvcxs5re.css'); */

.uni-toast-content .uni-sample-toast {
	color: #fff;
	font-size: 80rpx !important;
}
</style>
<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
/* #ifndef APP-NVUE */
@import '@/uni_modules/uview-ui/index.scss';
/* #endif */
</style>
